//
//  ConMyTeamController.m
//  Shengzhuli
//
//  Created by FXGuo on 17/3/26.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConMyTeamController.h"
#import "MyTeamNumCell.h"
#import "TeamNameCell.h"
#import "TeamMemberCell.h"
#import "ConEditCardViewController.h"
#import "PersonNetWork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "NewCardVc.h"

@interface ConMyTeamController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *teams;

@end

@implementation ConMyTeamController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的团队";
    [self setTableViews];
    [self getMyTeamList];
}

- (void)setTableViews
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-SafeAreaTopHeight) style:UITableViewStyleGrouped];
    tableView.backgroundColor = RGBColor(239, 239, 244);
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.sectionHeaderHeight = 0.01;
    tableView.sectionFooterHeight = 0.01;
    tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 0);
//    tableView.rowHeight = UITableViewAutomaticDimension;
//    tableView.estimatedRowHeight = 100;
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyTeamNumCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MyTeamNumCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeamNameCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeamNameCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeamMemberCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeamMemberCell class])];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}
#pragma mark - Table view data delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        return 0.01;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        return 120;
    }
    if (indexPath.row == 0) {
        return 40;
    }
    TeamModel *tModel = self.teams[indexPath.section-1];
    FriendsDetailModel *fModel = tModel.memberDtoList[indexPath.row-1];
    if (fModel.referrerPhone.length<11) {
        return 51;
    }
    return 71;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.teams.count+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    TeamModel *model = self.teams[section-1];
    return model.memberDtoList.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MyTeamNumCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyTeamNumCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.teams = self.teams;
        return cell;
    }
    if (indexPath.row == 0) {
        TeamNameCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeamNameCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.teams[indexPath.section-1];
        return cell;
    }
    TeamMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeamMemberCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.clipsToBounds = YES;
    TeamModel *tModel = self.teams[indexPath.section-1];
    cell.model = tModel.memberDtoList[indexPath.row-1];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0&&indexPath.row != 0) {
        TeamModel *tModel = self.teams[indexPath.section-1];
        FriendsDetailModel *fModel = tModel.memberDtoList[indexPath.row-1];
        PersonInfo *info = [[PersonInfo alloc]init];
        info.portraitUrl = fModel.portraitUrl;
        info.name = fModel.name;
        info.position = fModel.position;
        info.mobilePhone = fModel.mobilePhone;
        info.detailedAddress = fModel.detailedAddress;
        info.personalizedSignature = fModel.personalizedSignature;
        info.shareUrl = @"  ";
        NewCardVc *cardVc = [[NewCardVc alloc]init];
        cardVc.info = info;
        cardVc.isOther = YES;
        [self.navigationController pushViewController:cardVc animated:YES];
    }
}
- (void)getMyTeamList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showClearBgWaiter];
    [PersonNetWork getMyTeamListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self.teams addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (NSMutableArray *)teams
{
    if (!_teams) {
        _teams = [NSMutableArray array];
    }
    return _teams;
}


@end
